﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace SoundManager
{
    public class Ship : Microsoft.Xna.Framework.DrawableGameComponent
    {
        // Player's 3D model
        private Model model;

        // Used to change the position of the model
        private Matrix worldMatrix = Matrix.Identity;

        // Used to store the different possible textures a ship can have
        private Texture2D[] textures;

        public Vector3 position = Vector3.Zero;

        
        // Index for the currently used texture
        private int currentTexture = 0;

        // Constructor
        public Ship(Game game) : base(game) { }


        // Initializer
        public void Initialize(ref Model theModel, ref Texture2D[] theTextures, Vector3 startPosition)
        {
            model = theModel;
            textures = theTextures;

            // Done in order to have the ship start lifted off the plane
            UpdateWorldMatrix(startPosition);
        }


        public void UpdateWorldMatrix(Vector3 translateTo)
        {
            worldMatrix = //Matrix.CreateRotationZ(rotateBy) *
                        Matrix.CreateRotationX(-MathHelper.PiOver2) *
                        Matrix.CreateRotationY(MathHelper.Pi) *
                        Matrix.CreateTranslation(new Vector3(0, 1.5f, 0)) *
                        Matrix.CreateTranslation(translateTo);
        }

      

        public void Update(GameTime gameTime)
        {
            UpdateWorldMatrix(position);
            base.Update(gameTime);
        }


        public void Draw(GameTime gameTime, Matrix view, Matrix projection)
        {
            // Draw the model
            DrawModel(model, worldMatrix, view, projection);
        }

        /// <summary>
        /// Custom function to draw our 3D models
        /// </summary>
        /// <param name="model">The 3D model we want to draw.</param>
        /// <param name="world">The world matrix for the corresponding model.</param>
        /// <param name="view">The view matrix for the corresponding.</param>
        /// <param name="projection">The projection matrix for the corresponding.</param>
        private void DrawModel(Model model, Matrix world, Matrix view, Matrix projection)
        {
            foreach (ModelMesh mesh in model.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.EnableDefaultLighting();
                    effect.Texture = textures[currentTexture];
                    effect.World = world;
                    effect.View = view;
                    effect.Projection = projection;
                }

                mesh.Draw();
            }
        }

        public Model getModel()
        {
            return model;
        }

        public Matrix getWorldMatrix()
        {
            return worldMatrix;
        }

        public Vector3 getPosition()
        {
            return position;
        }

        public void updatePosition(Vector3 add)
        {
            position += add;
        }

        public void shoot()
        {
            SoundManager.GetInstance().playSound(position, SoundManager.FIRE);
        }

        public void reload()
        {
            SoundManager.GetInstance().playSound(position, SoundManager.LOAD);
        }
    }
}